<template>
	<view>
		<view class="search-box-container">
			<uni-search-bar @input="input" cancelButton='none' radius='100'></uni-search-bar>
		</view>
		
		
		
		<view v-if="searchResult.length!==0">
			<view class="container-box" v-for="(item,index) in searchResult" :key="index"
				@click="toSearchDetail(item.goods_id)">
				<view class="goods_name">{{item.goods_name}}</view>
				<uni-icons @click="clean" type="arrow-right" size="16"></uni-icons>
			</view>
		</view>

		<view class="histroy_container" v-else>
			<view class="histroy_box">
				<view class="histroy_result">搜索历史</view>
				<uni-icons type="trash" size="16"></uni-icons>
			</view>
			<!-- 历史内容 -->
			<view class="histroy_list">
				<uni-tag @click="toGoodsDetail(item)" :text="item" v-for="(item,index) in histories" :key="index"></uni-tag>
			</view>
		</view>


	</view>
</template>

<script>
	import {
		uniSearchBar,
		// uniTag
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniSearchBar,
			// uniTag
		},
		computed: {
			histories() {
				return [...this.histroyList].reverse()
			}
		},
		onLoad() {
		this.histroyList=JSON.parse(uni.getStorageSync('kw') || '[]')
			
		},
		data() {
			return {
				timer: null,
				kw: '',
				searchResult: [{
						goods_name: '会不都是l拉拉',
						goods_id: 20222
					},
					{
						goods_name: 'laladshjasb错别字v还是不会不都是l拉拉',
						goods_id: 20230
					},
					{
						goods_name: 'dVS收到分手的VSgs拉拉',
						goods_id: 20231
					},
					{
						goods_name: 'ad是否的士速递VS打错字v秩序小吃吧拉拉',
						goods_id: 20232
					},
					{
						goods_name: 'afa时高时低归属感湿地公园突然被拉拉',
						goods_id: 20233
					},
					{
						goods_name: 'lala日光灯管孙子女您只需你仨GV和SVNSVN少女l拉拉',
						goods_id: 20230
					},
					{
						goods_name: 'lala但是大部分对不对方便的突然爸爸发吧发吧l拉拉',
						goods_id: 20230
					}, {
						goods_name: 'lalal怪不得发个红包电饭锅不发达滚吧代发表达法拉拉',
						goods_id: 20230
					},

				],
				histroyList: ['1', 'a', 'asd']
			};
		},
		methods: {
			// 根据历史列表跳转到商品详情
			toGoodsDetail(item){
				uni.navigateTo({
					url:"/subpkg/goods_details/goods_details?query="+item
				})
			},
			// 清空历史列表
			clean(){
				this.histroyList =[]
				uni.setStorageSync('kw','[]')
			},
			// 根据搜索结果，跳转到详情页面
			toSearchDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item
				})
			},
			// 输入搜索内容
			input(e) {
				// 搜索结果
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e.value
					this.getSearchResult()
				}, 500)

			},
			async getSearchResult() {
				if (this.kw.length === 0) {
					this.searchResult = []
					return
				}
				const {
					data: res
				} = uni.$http.get('/api/', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message;
				this.saveSearchHistroy()
			},
			// 搜索结果存储到搜索历史中
			saveSearchHistroy() {
				// this.histroyList.push(this.kw)
				const set = new Set(this.histroyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.histroyList = Array.from(set)
				// 将搜索历史进行本地存储，数组通过JSON转换成字符串
				uni.setStorageSync('kw',JSON.stringify(this.histroyList))
			},
		}
	}
</script>

<style lang="scss">
	.search-box-container {
		position: static;
		top: 0;
		z-index: 999;

	}

	.container-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 5px 5px;
		font-size: 12px;
		border-bottom: 1px solid #c3c3c3;

		.goods_name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}

	.histroy_container {
		padding: 0px 5px;

		.histroy_box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			// padding: 5px 5px;
			border-bottom: 1px solid #efefef;

			.histroy_result {
				font-size: 12px;
			}
		}

		.histroy_list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin: 0 5px;
			}
		}
	}
</style>